package persistency;
/**
 * @ Authors:
 * 
 * @Veronique De Laet
 * @Johan Willems
 * 
 * @version 2012
 */

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import model.Price;

public class Price_CRUD implements Repository<Price> {
	
	//private static final String PERSISTENCE_UNIT_NAME = "MiniServiceDesk";

	
	@Override  
	public void insert(Price entity) throws PersistencyException {
		  EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager(); 
		  EntityTransaction tx = em.getTransaction(); 
		  try{
		  tx.begin();
		  em.persist(entity); 
		  em.flush();
		  tx.commit(); 
		  }
		  catch(Exception ex)
		  {
			  ex.printStackTrace();
		  }
		  finally{
			  em.close();
		  }
	  }
	
	@Override
	public void update(Price entity) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager(); 
		EntityTransaction tx = em.getTransaction();
		  try{
			  tx.begin();
			  em.merge(entity);
			  tx.commit();		  
		  }
		  catch(Exception ex)
		  {
			  ex.printStackTrace();
		  }
		  finally{
			  em.close();
		  }
	}
	
	@Override
	public void delete(Price entity) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager(); 
		EntityTransaction tx = em.getTransaction();
		  try{
			  tx.begin();
			  Price dPrice = getById(entity.getPriceId());
			  em.remove(dPrice);
			  tx.commit();
		  }
		  catch(Exception ex)
		  {
			  ex.printStackTrace();
		  }
		  finally{
			  em.close();
		  }
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Price> getAll() throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager(); 
		EntityTransaction tx = em.getTransaction(); 
		try{
			tx.begin(); 
			List<Price> allPrices = em.createQuery("select p from Price p") .getResultList(); 
			tx.commit();
			return allPrices; 
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally{
			em.close();
		}
		return null;
	}
	
	public List<String> getAllUnitCodes(){
		EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager(); 
		EntityTransaction tx = em.getTransaction(); 
		try{
			tx.begin(); 
			List<String> allUnitCodes = em.createQuery("select distinct p.unitCode from Price p").getResultList(); 
			tx.commit();
			return allUnitCodes; 
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally{
			em.close();
		}
		return null;
		
	}
	
	public List<Integer> getAllPriceId(){
		EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager(); 
		EntityTransaction tx = em.getTransaction(); 
		try{
			tx.begin(); 
			List<Integer> allPriceId = em.createQuery("select distinct p.priceId from Price p").getResultList(); 
			tx.commit();
			return allPriceId; 
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally{
			em.close();
		}
		return null;
		
	}
	
	@Override
	public Price getById(Object id) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager(); 
		EntityTransaction tx = em.getTransaction(); 
		try{
			tx.begin();
			Price gPrice = em.find(Price.class, id);
			tx.commit();
			return gPrice;
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally{
			em.close();
		}
		return null;
	}
	
	@Override
	public int getCount() throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager(); 
		EntityTransaction tx = em.getTransaction(); 
		try{
			tx.begin();
			int pCount = Integer.parseInt(em.createQuery("Select count(p.priceId)from Price p")
					.getSingleResult().toString());
			tx.commit();
			return pCount;
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally{
			em.close();
		}
		return 0;
	}
	
	//nog nakijken of voldoende om id mee te geven
	 public void disablePrice(Object id) { 
		  EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager(); 
		  EntityTransaction tx = em.getTransaction(); 
		  try
		  {
			  tx.begin();
				Price disPrice = getById(id);
				disPrice.setActive(false);
				em.merge(disPrice);
				tx.commit();
		  }catch(Exception ex)
			{
				ex.printStackTrace();
			}
			finally{
				em.close();
			}
		}
	  
	 @SuppressWarnings("unchecked")
	  public List<Price> getAllActivePrices() { 
		  EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager(); 
		  EntityTransaction tx = em.getTransaction(); 
		  try{
			  tx.begin(); 
			  List<Price> allActivePrices = em.createQuery("select p from Price p where p.active = true") .getResultList(); 
			  tx.commit();
			  return allActivePrices; 
		  }catch(Exception ex)
		  {
			ex.printStackTrace();
		  }
		  finally{
			em.close();
		  }
		  return null;
	  }
}
